﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class Locales : AdminPage, ILocalesView
    {
        private LocalesPresenter _presenter;

        #region ILocalesView Members

        public void SetLocales(IList<Locale> locales)
        {
            rpLocales.DataSource = locales;
            rpLocales.DataBind();
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/Locales.xml"; }
        }


        public void SetReplacements(IList<Locale> locales, string name)
        {
            ddlReplacements.Items.Clear();
            foreach (Locale l in locales)
                ddlReplacements.Items.Add(new ListItem(l.GetTranslation(Localization).Name, l.ID.ToString()));
            ddlReplacements.Items.Insert(0, new ListItem("- " + GetText(this, "SelectReplacement") + " -", "-1"));
            litReplacementMessage.Text = GetText(this, "ReplacementMessage").Replace("[[Name]]", name);
            litReplacementLabel.Text = GetText(this, "ReplacementLabel").Replace("[[Name]]", name);
            upnlReplacements.Update();
            string script =
                "cms.Dialogs.Confirm(replaceLocale, 'Info', null, 'OK', 'Cancel', true, null, 'replaceLocale');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "replaceLocale", script, true);
        }


        public void DisplayUnableToDeleteAlert(string name)
        {
            string script = "cms.Dialogs.Alert(null, '" + GetText(this, "DeleteAlertHeader") + "', '" +
                            GetText(this, "DeleteAlertMessage").Replace("[[Name]]", name) + "', '" +
                            GetText(this, "DeleteAlertOK") + "');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "unableToDeleteAlert", script, true);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new LocalesPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('localePaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }


        protected void rpLocales_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            Locale locale = (Locale) e.Item.DataItem;
            Literal litID = (Literal) e.Item.FindControl("litID");
            Literal litName = (Literal) e.Item.FindControl("litName");
            Literal litUrlCode = (Literal)e.Item.FindControl("litUrlCode");
            Literal litCultureCode = (Literal)e.Item.FindControl("litCultureCode");
            litID.Text = "<span id=\"D" + locale.ID + "\">";
            litName.Text = locale.GetTranslation(Localization).Name;
            litUrlCode.Text = locale.UrlCode;
            litCultureCode.Text = locale.CultureCode;
        }


        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteLocale(int.Parse(hidDeleteLocaleID.Value.Substring(1)));
            upnlLocales.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initLocaleList", "initLocaleList();",
                                                            true);
        }


        protected void btnReplace_Click(object sender, EventArgs e)
        {
            if (hidReplacementID.Value != "")
                _presenter.ReplaceAndDeleteLocale(int.Parse(hidDeleteLocaleID.Value.Substring(1)),
                                                   int.Parse(hidReplacementID.Value));
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initLocaleList", "initLocaleList();",
                                                            true);
            upnlLocales.Update();
        }
    }
}